//
//  ProductXlViewController.m
//  CMVRFForIOS
//
//  Created by zhangnorman on 15/12/27.
//  Copyright © 2015年 hvacapp. All rights reserved.
//

#import "ProductXlViewController.h"
#import "productData.h"
#import "ProjectData.h"
#import "ProductXsViewController.h"
#import "ProductListViewController.h"
#import "CacheData.h"

@interface ProductXlViewController ()

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;

@end

@implementation ProductXlViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initData];
    [self setupViews];
}

/**
 *  初始化数据
 */
- (void)initData {
    self.dataSource = [NSMutableArray new];
}

#pragma mark - 初始化视图
- (void)setupViews{    
    self.tableView = [[UITableView alloc] initWithFrame:[UIView frameWithOutNav] style:UITableViewStylePlain];
    self.tableView.backgroundColor = CM_TABLE_BG_COLOR;
    [self.tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}

#pragma mark - 加载数据
/**
 *  加载视图所有的数据
 */
- (void)loadData {
    //[self loadNationData];
    [self loadProductSeries:self.product_type];
}


-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];;
    [self loadData];
}


/**
 *  获得可选得产品系列列表
 *
 */
- (void)loadProductSeries:(NSString *)tag {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:self.product_type forKey:@"product_type"];
    
    [HttpClient getWithUrl:HTTP_GET_EQUIP_SERIES_QUERY paramDic:param resultClass:[ProductData class] success:^(ProductData *obj) {
        if (obj.series != nil && obj.series.count > 0) {
            [self.dataSource removeAllObjects];
            [self.dataSource addObjectsFromArray:obj.series];
            [self.tableView reloadData];
        }
        CMLog(@"%@", obj);
    } failure:^(NSError *error) {
        CMLog(@"%@", error);
    }];
}


#pragma mark - 事件响应

#pragma mark - UIAlertViewDelegate 代理

#pragma mark - tableView显示设置数据
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"XLCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.textColor = CM_TABLE_TITLE_COLOR;
    cell.detailTextLabel.textColor = CM_TABLE_INFO_COLOR;
    NSDictionary *item = [self.dataSource objectAtIndex:indexPath.row];
    cell.textLabel.text = [item objectForKey:@"Series"];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ProductListViewController *vc = [ProductListViewController new];
    NSDictionary *item = [self.dataSource objectAtIndex:indexPath.row];
    vc.series = [item objectForKey:@"Series"];
    vc.situation_type = 3;
    vc.title = [item objectForKey:@"Series"];
    vc.product_type = self.product_type;
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
